const state = {
  devices: [],
  loading: false,
  error: null
};

const mutations = {
  SET_DEVICES(state, devices) {
    state.devices = devices;
    state.loading = false;
    state.error = null;
  },
  SET_LOADING(state, loading) {
    state.loading = loading;
  },
  SET_ERROR(state, error) {
    state.error = error;
    state.loading = false;
  }
};

const actions = {
  async fetchDevices({ commit }) {
    commit('SET_LOADING', true);
    try {
      const response = await axios.get('/api/devices');
      commit('SET_DEVICES', response.data);
    } catch (error) {
      commit('SET_ERROR', error.message);
    }
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};